package com.xiaoyi.service.impl;

import com.xiaoyi.dao.UserInfoDao;
import com.xiaoyi.dao.impl.UserInfoDaoImpl;
import com.xiaoyi.entity.UserInfo;
import com.xiaoyi.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

public class UserInfoServiceImpl implements UserInfoService {
    private UserInfoDao userInfoDao=new UserInfoDaoImpl();
    @Override
    public String register(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        //获取前端页面输入的数据
        String nickName = req.getParameter("nickName");
        String userName = req.getParameter("userName");
        String pwd = req.getParameter("pwd");
        String phone = req.getParameter("phone");
        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setNickName(nickName);
        userInfo.setPassword(pwd);
        userInfo.setTel(phone);
        int insert = userInfoDao.insert(userInfo);

        return insert>0?"/pages/user/login.jsp":"/pages/user/register.jsp";
    }

    @Override
    public String login(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String userName = req.getParameter("userName");
        String pwd = req.getParameter("pwd");
        UserInfo userInfo = userInfoDao.selectUserInfoByUserNameAndPwd(userName, pwd);
        if(null!=userInfo){
            req.getSession().setAttribute("userInfo",userInfo);
            return "/userHome.do";
        }else{
            return "/pages/user/login.jsp";
        }
    }
}
